package com.example.websocket_service.config;


public class ApiResult <T> {
	// 单列，仅仅返回成功和失败，不要对它赋值
	public final static ApiResult success = new ApiResult(0, "success", null);
	// 单列，仅仅返回成功和失败，不要对它赋值
	public final static ApiResult fail = new ApiResult(1, "fail", null);

	private Integer code;
	private String msg;
	private T data;

	public ApiResult() {
	}

	public ApiResult(Integer code) {
		this.code = code;
	}

	public ApiResult(Integer code, String error) {
		this.code = code;
		this.msg = error;
	}

	public ApiResult(Integer code, T data) {
		this.code = code;
		this.data = data;
	}

	public ApiResult(Integer code,  String msg,T data) {
		this.code = code;
		this.data = data;
		this.msg = msg;
	}

	public String getMsg() {
		if (msg == null || "".equals(msg)) {
			return code.toString();
		}
		return msg;
	}

	public ApiResult msg(String msg) {
		this.msg = msg;
		return this;
	}

	public Object getData() {
		return data;
	}

	public ApiResult data(T data) {
		this.data = data;
		return this;
	}

	public int getCode() {
		return code;
	}

	public ApiResult code(Integer code) {
		this.code = code;
		return this;
	}

	public static ApiResult ok() {
		return new ApiResult(0);
	}

	public static ApiResult ok(Object data) {
		return new ApiResult(0, data);
	}

	public static ApiResult exception(String msg) {
		return new ApiResult(1, null, msg);
	}

	public static ApiResult exception() {
		return new ApiResult(1);
	}

	public static ApiResult build() {
		return new ApiResult();
	}
}
